package cn.rdq.iknessplatform.service.impl;

import cn.rdq.api.po.Drama;
import cn.rdq.api.po.UserPurse;
import cn.rdq.api.vo.UserPursueVo;
import cn.rdq.common.status.PurseStatus;
import cn.rdq.dao.UserPurseMapper;
import cn.rdq.iknessplatform.service.UserPursueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserPursueServiceImpl extends ServiceImpl<UserPurseMapper, UserPurse> implements UserPursueService {

    @Resource
    private UserPurseMapper userPurseMapper;


    @Override
    public List<Drama> userPurseByUserId(UserPursueVo userPursueVo) {
        PageHelper.startPage(userPursueVo.getCurrentPage(), userPursueVo.getPageSize());
        return userPurseMapper.listUserPursue(userPursueVo.getUserId(), PurseStatus.UNWATCHED.getKey());
    }

    @Override
    public List<Drama> userWatchedByUserId(UserPursueVo userPursueVo) {
        PageHelper.startPage(userPursueVo.getCurrentPage(), userPursueVo.getPageSize());
        return userPurseMapper.listUserPursue(userPursueVo.getUserId(), PurseStatus.WATCHED.getKey());
    }

    @Override
    public UserPurse getWatched(UserPurse userPurse) {
        QueryWrapper<UserPurse> wrapper = new QueryWrapper<>();
        wrapper.select("id", "watched")
                .eq("user_id", userPurse.getUserId())
                .eq("drama_id", userPurse.getDramaId());
        return getOne(wrapper);
    }

    @Override
    public boolean saveOrUpdateWatched(UserPurse userPurse) {
        UpdateWrapper<UserPurse> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id", userPurse.getUserId())
                .eq("drama_id", userPurse.getDramaId())
                .set("watched", userPurse.getWatched());
        return saveOrUpdate(userPurse, wrapper);
    }
}
